package LoginEvent;

import util.db.DataSourceUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;

/**
 * @author lenovo
 */
// 使用HttpServlet需要从tomcat/lib中导入两个jar包
public class Register extends HttpServlet {
    /*
     * 因为要处理HTML表单提交过来的数据，所以这里使用post请求
     * */

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement ps = null;

        String uuid = UUID.randomUUID().toString().replace("-", "");

        //这些数据通过注册界面的表单提交过来
        String userName=request.getParameter("userName");
        String loginName=request.getParameter("loginName");
        String userPass=request.getParameter("userPass");
        String confirmPass=request.getParameter("confirmPass");
        String role=request.getParameter("role");
        String birth=request.getParameter("birth");
        String phone=request.getParameter("phone");
        String email=request.getParameter("email");


        String sql = "insert into shopadmin values(?,?,?,?,?,?,?,?)";
        try{
            conn = DataSourceUtil.getConnection();
            //2.创建语句对象
            //预编译
            ps=conn.prepareStatement(sql);
            //3.填充占位符
            ps.setString(1,uuid);
            ps.setString(2,userName);
            ps.setString(3,loginName);
            ps.setString(4,userPass);
            ps.setString(5,role);
            ps.setString(6,birth);
            ps.setString(7,phone);
            ps.setString(8,email);
            //4.执行sql语句
            System.out.println();
            ps.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            //4. 释放资源
            DataSourceUtil.close(conn, ps);
        }
    }
}
